package com.mzy.datepackage.M2022.Jun.tw;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/6/20 9:23
 * @Version 1.0
 */
@Slf4j
public class Demo {
    private Map<String, ModelEnumTest> modelEnumMap;

    public Demo() {
        modelEnumMap = new HashMap<>();
        modelEnumMap.put("ip", ModelEnumTest.ALIASES_IP);
        modelEnumMap.put("schedule", ModelEnumTest.ALIASES_SCHEDULE);
        modelEnumMap.put("ALIASES_Name", ModelEnumTest.ALIASES_Name);
        modelEnumMap.put("ALIASES_age", ModelEnumTest.ALIASES_age);
        modelEnumMap.put("info", ModelEnumTest.ALIASES_ipInfo);
    }

    @Test
    public void test(){
        V2ConfigParams v2ConfigParams = new V2ConfigParams();
        v2ConfigParams.setALIASES_Name("小明");
        ArrayList<Ip> objects = new ArrayList<>();
        Ip ip = new Ip("aa","ss");
        Ip ip2 = new Ip("aa","ss");
        objects.add(ip);
        objects.add(ip2);
        v2ConfigParams.setIp(objects);
        try {
            for (String key : modelEnumMap.keySet()) {
                String[] split = key.split(">");
                System.out.println(Arrays.toString(split) + split.length);
                Object lastTag = findLastTag(split, 0, v2ConfigParams);

                System.out.println("findLastTag"+lastTag);

                if (lastTag != null) {
                    log.info("模块[{}],查找到原配置:{}", modelEnumMap.get(key), JSON.toJSONString(lastTag));
                } else {
                    ModelEnumTest modelEnum = modelEnumMap.get(key);
                    log.info("模块[{}],原配置为空，写入默认配置:{}", modelEnumMap.get(key), JSON.toJSONString(modelEnum.getDefaultValue()));
                }

            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 模拟
     * @param tags
     * @param nowTagIndex
     * @param object
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    private Object findLastTag(String[] tags, int nowTagIndex, Object object) throws NoSuchFieldException, IllegalAccessException {
        Class<?> clazz = object.getClass();
        Field field = clazz.getDeclaredField(tags[nowTagIndex]);
        field.setAccessible(true);
        Object result = field.get(object);
        if (result == null) {
            try {
                if (field.getType().equals(List.class)) {
                    result = new ArrayList<>();
                    System.out.println(result.toString());
                } else {
                    result = field.getType().getConstructor().newInstance();
                    System.out.println(result.toString());
                }
            } catch (InstantiationException e) {
                log.error("初始化节点时创建对象异常", e);
            } catch (InvocationTargetException e) {
                log.error("初始化节点时创建对象异常", e);
            } catch (NoSuchMethodException e) {
                log.error("初始化节点时创建对象异常", e);
            }
        }
        //代表进行到最后
        if (tags.length - 1 == nowTagIndex) {
            return result;
        } else {//如果没有进行到最后，则递归查询
            return findLastTag(tags, ++nowTagIndex, result);
        }
    }
}
